#!/bin/sh

# errors shouldn't cause script to exit
set +e 

# add rserver user account
useradd -r rstudio-server
groupadd -r rstudio-server

# create softlink to admin script in /usr/sbin
ln -f -s ${CMAKE_INSTALL_PREFIX}/bin/rstudio-server /usr/sbin/rstudio-server

# create config directory and default config files
mkdir -p /etc/rstudio
if ! test -f /etc/rstudio/rserver.conf
then
  printf '# Server Configuration File\n\n' > /etc/rstudio/rserver.conf
fi
if ! test -f /etc/rstudio/rsession.conf
then
  printf '# R Session Configuration File\n\n' > /etc/rstudio/rsession.conf
fi

# create var directories
mkdir -p /var/run/rstudio-server
mkdir -p /var/lock/rstudio-server
mkdir -p /var/log/rstudio-server
mkdir -p /var/lib/rstudio-server
mkdir -p /var/lib/rstudio-server/conf
mkdir -p /var/lib/rstudio-server/body
mkdir -p /var/lib/rstudio-server/proxy

# suspend all sessions
rstudio-server force-suspend-all

# create openssl softlinks based off SuSE version 
if test -f /etc/SuSE-release
then
  if test -d /usr/lib64 || test -d /lib64
  then
    architecture_dir='lib64'
  else
    architecture_dir='lib'
  fi

  compare_versions() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
  version_number=$(cat /etc/SuSE-release | grep "VERSION" | sed 's/[^0-9,.]*//g')

  # verify if release version number is greater than 11
  if compare_versions $version_number 11
  then
    ln -s -f /$architecture_dir/libssl.so.1.0.0 ${CMAKE_INSTALL_PREFIX}/bin/libssl.so.6
    ln -s -f /$architecture_dir/libcrypto.so.1.0.0 ${CMAKE_INSTALL_PREFIX}/bin/libcrypto.so.6
  else
    ln -s -f /usr/$architecture_dir/libssl.so.0.9.8 ${CMAKE_INSTALL_PREFIX}/bin/libssl.so.6
    ln -s -f /usr/$architecture_dir/libcrypto.so.0.9.8 ${CMAKE_INSTALL_PREFIX}/bin/libcrypto.so.6
  fi
fi

# add systemd, upstart, or init.d script and start the server
INIT_SYSTEM=`cat /proc/1/comm 2>/dev/null`
if test "$INIT_SYSTEM" = "systemd"
then
   # remove any previously existing init.d based scheme
   service rstudio-server stop 2>/dev/null
   rm -f /etc/init.d/rstudio-server
   
   systemctl stop rstudio-server.service 2>/dev/null
   systemctl disable rstudio-server.service 2>/dev/null
   cp ${CMAKE_INSTALL_PREFIX}/extras/systemd/rstudio-server.redhat.service /etc/systemd/system/rstudio-server.service
   systemctl daemon-reload
   systemctl enable rstudio-server.service
   systemctl start rstudio-server.service
   sleep 1
   systemctl --no-pager status rstudio-server.service
elif test -d /etc/init/
then
   # remove any previously existing init.d based scheme
   service rstudio-server stop 2>/dev/null
   rm -f /etc/init.d/rstudio-server

   cp ${CMAKE_INSTALL_PREFIX}/extras/upstart/rstudio-server.redhat.conf /etc/init/rstudio-server.conf
   initctl reload-configuration
   initctl stop rstudio-server 2>/dev/null
   initctl start rstudio-server
else
   if test -f /etc/SuSE-release
   then
      cp ${CMAKE_INSTALL_PREFIX}/extras/init.d/suse/rstudio-server /etc/init.d/
   else
      cp ${CMAKE_INSTALL_PREFIX}/extras/init.d/redhat/rstudio-server /etc/init.d/
   fi
   chmod +x /etc/init.d/rstudio-server
   chkconfig --add rstudio-server
   service rstudio-server stop 2>/dev/null
   service rstudio-server start
fi

# add pam profile
if [ ! -e /etc/pam.d/rstudio ]
then
   cp ${CMAKE_INSTALL_PREFIX}/extras/pam/rstudio /etc/pam.d/
fi

# clear error termination state
set -e
